package com.android.server.mock;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.util.Log;

public class FileUtil {
    public static final String TAG = "FileUtil";

    public static final int STRING_MAX = 2097152;

    public static String getFileContent(File file, boolean deleteFile) {
        String content = null;
        int start = 0;
        if (file.exists()) {
            int size = (int) file.length();
            Log.d(TAG, "getFileContent: size " + size);
            if (file.toString().startsWith("/proc")) {
                size = 10240;
            }
            if ((size > 0) && (size < STRING_MAX)) {
                byte[] tmp = new byte[size];
                FileInputStream fis = null;
                try {
                    int type = -1;
                    int idx = 0;
                    int len = 0;
                    fis = new FileInputStream(file);
                    if (fis != null) {
                        while ((len = fis.read(tmp, idx, (size - idx))) != -1) {
                            //Log.d(TAG,  "getFileContent: file path=" + file.getAbsolutePath() +
                            //	", idx=" + idx + ", len=" + len + ", size=" + size);
                            idx += len;
                            if (idx == size) {
                                break;
                            }
                        }
                        if ((size > 3) && (tmp[0] == (byte) 0xef) && (tmp[1] == (byte) 0xbb) && (tmp[2] == (byte) 0xbf)) {
                            start = 3;
                            content = new String(tmp, start, size - start);
                        } else if ((size > 3) && (tmp[0] == (byte) 0xb0) && (tmp[1] == (byte) 0xfc) && (tmp[2] == (byte) 0xc3)) {
                            start = 0;
                            content = new String(tmp, start, size - start, "ISO-8859-1");
                        } else {
                            start = 0;
                            content = new String(tmp, start, size - start);
                        }
                        //fis.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d(TAG, "getFileContent: file read exception " + e);
                } finally {
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            Log.d(TAG, "getFileContent: fis.close exception " + e);
                        }
                    }
                }
            }
            if (deleteFile == true) {
                file.delete();
            }
        } else {
            Log.d(TAG, "getFileContent: file not exist ");
        }
        return content;
    }

    public static boolean saveToFile(String filePath, byte[] data) {
        boolean ret = false;
        if ((filePath != null) && (data != null)) {
            File file = new File(filePath);
            try {
                if (file.exists()) {
                    file.delete();
                }
                file.createNewFile();
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(data);
                fos.flush();
                fos.close();
                ret = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    public static boolean saveToFile(String filePath, String data) {
        boolean ret = false;
        if (data != null) {
            ret = saveToFile(filePath, data.getBytes());
        }
        return ret;
    }
}
